##=============================================================================
## Appendix Figure 21
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse")

ipak(packages)

##---------
# Load data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("~/Desktop/clean_ukraine_data.RData")

# N.B. neutral strat question only available in wave 1
dat1 <- dat1 %>% 
  mutate(head_down = case_when(
    neutral_strat == 1 ~ 4,
    neutral_strat == 2 ~ 3, 
    neutral_strat == 3 ~ 2, 
    neutral_strat == 4 ~ 1,
    neutral_strat == 5 ~ NA,
    neutral_strat == 6 ~ NA)) %>% 
  mutate(head_down = factor(head_down, levels = c(1,2,3,4), ordered = TRUE))

str(dat1$head_down)
table(is.na(dat1$head_down)) # 5 percent missingness

fit1 <- clmm(head_down ~ 1 + survival_mindset + per_idx_invcov + fam_idx_invcov + 
               frontline_b + displaced + sex + rus_ethnic + age + edu + job + 
               ln_viina_two_months + finances + (1 + survival_mindset + per_idx_invcov +
                                                   fam_idx_invcov | adm1_en), 
             data = dat1)


tab_model(fit1)
AIC(logLik(fit1))

# Effect of Survival Mindset on Support for 'Keeping one's head down' (Wave I only)
plot_model(fit1, 
           sort.est = TRUE, 
           vline.color = "red",
           show.values = TRUE, 
           value.offset = .3,
           axis.labels = "", 
           title = "Effect of Survival Mindset on Support for 'Keeping one's head down' (Wave I only)")

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================